package com.jdone.compus.controller;

import com.jdone.compus.dto.ApiResponse;
import com.jdone.compus.model.BlowQuickDuration;
import com.jdone.compus.service.BlowQuickDurationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/blow/quick_durations")
@Api(value = "吹风快捷时长设置", tags = "吹风快捷时长设置")
public class BlowQuickDurationController {

    @Autowired
    private BlowQuickDurationService service;

    @ApiOperation("新增快捷时长选项")
    @PostMapping("/end_back/add")
    public ApiResponse add(@RequestBody BlowQuickDuration dto) {
        return ApiResponse.success(service.create(dto));
    }

    @ApiOperation("根据ID查询快捷时长选项")
    @GetMapping("/end_back/find/{id}")
    public ApiResponse getById(@PathVariable Long id) {
        try {
            return ApiResponse.success(service.getById(id));
        } catch (RuntimeException ex) {
            return ApiResponse.error(ex.getMessage());
        }
    }

    @ApiOperation("分页查询所有快捷时长选项，按 deviceType 可选过滤")
    @GetMapping("/findAll")
    public ApiResponse list(
            @ApiParam("页码，从 1 开始") @RequestParam(defaultValue = "1") int page,
            @ApiParam("每页条数") @RequestParam(defaultValue = "10") int size,
            @ApiParam("设备类型 1-插板 2-吹风机（可不传，查询所有）")
            @RequestParam(required = false) Integer deviceType) {

        Pageable pageable = PageRequest.of(page - 1, size);
        Page<BlowQuickDuration> pageResult = service.getAll(deviceType, pageable);
        return ApiResponse.success(pageResult);
    }
    @ApiOperation("修改快捷时长选项")
    @PutMapping("/end_back/update/{id}")
    public ApiResponse update(@PathVariable Long id, @RequestBody BlowQuickDuration dto) {
        try {
            return ApiResponse.success(service.update(id, dto));
        } catch (RuntimeException ex) {
            return ApiResponse.error(ex.getMessage());
        }
    }

    @ApiOperation("删除快捷时长选项")
    @DeleteMapping("/end_back/delete/{id}")
    public ApiResponse delete(@PathVariable Long id) {
        try {
            service.delete(id);
            return ApiResponse.success("删除成功");
        } catch (RuntimeException ex) {
            return ApiResponse.error(ex.getMessage());
        }
    }
}

